<?php

// +----------------------------------------------------------------------
// | Fanwe 方维众筹商业系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 甘味人生(526130@qq.com)
// +----------------------------------------------------------------------

class XiacaiRedisService extends BaseRedisService {

    //var $user_db; //:user_id  hash数据
    //var $xiacai_hash_db; //所有会员数据 user_id hash数据 存储在线数据
    //var $user_robot_db; //user_id  set数据 ,机器人的集合
    var $xiacai_guessing_db; // 每个瞎猜ID对应的所有竞猜项目

    /**
      +----------------------------------------------------------
     * 架构函数
      +----------------------------------------------------------
     * @access public
      +----------------------------------------------------------
     */

    public function __construct() {
        parent::__construct();
        $this->xiacai_guessing_db = $this->prefix . 'xiacai_guessing:';
    }

    /*
     * 添加视频
     * hash数据存储
     */

    public function insert_db($xiacai_id, $data) {

        $pipe = $this->redis->multi();

        if (!$xiacai_id) {
            $xiacai_id = $this->get_xiacai_id();
        } else {
            $pipe->hSet($this->auto_id_db, 'xiacai_id', $xiacai_id);
        }
        $data['id'] = $xiacai_id;
        filter_null($data);
        $this->redis->hMSet($this->xiacai_db . $xiacai_id, $data);
        $this->redis->hMSet($this->xiacai_hash_db, array($xiacai_id => json_encode($data)));
        $replies = $pipe->exec();
        if ($replies[0]) {
            return $xiacai_id;
        } else {
            return 0;
        }
    }

    /*
     * 更新视频信息
     * $user_id
     * $data 要更新的数组array('nick_name'=>$name)
     */

    public function update_db($xiacai_id, $data) {

        $xiacai_info = $this->redis->hGetAll($this->xiacai_db . $xiacai_id);

        $xiacai_info = array_merge($xiacai_info, $data);

        filter_null($data);
        $pipe = $this->redis->multi();
        $pipe->hMSet($this->xiacai_db . $xiacai_id, $data);
        $pipe->hMSet($this->xiacai_hash_db, array($xiacai_id => json_encode($xiacai_info)));
        $replies = $pipe->exec();

        return $replies;
    }

    public function del_db($xiacai_id) {
        $this->redis->hDel($this->xiacai_hash_db, $xiacai_id);
        $this->redis->delete($this->xiacai_db . $xiacai_id);
    }

    public function getGuessing($xiacai_id) {
        return $this->redis->hGetAll($this->xiacai_guessing_db . $xiacai_id);
    }

    public function setGuessing($xiacai_id, $data) {
        $list = array();
        foreach ($data as $row) {
            $list[$row['id']] = json_encode($row);
        }
        $this->redis->hMSet($this->xiacai_guessing_db . $xiacai_id, $list);
    }

    public function delGuessing($xiacai_id) {
        $this->redis->delete($this->xiacai_guessing_db . $xiacai_id);
    }

    /*
     * 会员相关字段自增 或者 自减
     */

    public function inc_field($xiacai_id, $field, $val) {
        return $this->redis->hIncrBy($this->xiacai_db . $xiacai_id, $field, $val);
    }

    /*
     * 获取单个字段
     */

    public function getOne_db($xiacai_id, $field) {
        return $this->redis->hGet($this->xiacai_db . $xiacai_id, $field);
    }

    /*
     * 获取多个字段
     * $fields 为空的话，则是获取所有
     */

    public function getRow_db($xiacai_id, $fields = '') {
        if (!$fields) {
            return $this->redis->hGetAll($this->xiacai_db . $xiacai_id);
        } else {
            return $this->redis->hMGet($this->xiacai_db . $xiacai_id, $fields);
        }
    }

    /*
     * 根据xiacai_id 批量获取瞎猜信息 $xiacai_list = array(11,12,13);
     */

    public function get_m_xiacai($xiacai_list) {
        $arrout = array();
        $xiacai_list_array = $this->redis->hMGet($this->xiacai_hash_db, $xiacai_list);
        if (is_array($xiacai_list_array)) {
            foreach ($xiacai_list_array as $k => $v) {
                if ($v) {
                    $xiacai_info = json_decode($v, true);
                    $xiacai_info['id'] = $k;
                    //$xiacai_info['image_url'] = get_spec_image($xiacai_info['image_url']);
                    $arrout[] = $xiacai_info;
                }
            }
        }
        return $arrout;
    }

}

//类定义结束
?>